package com.hwl.trade.controller;


import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.result.ResultCodeEnum;
import com.hwl.commonutils.utils.JwtInfo;
import com.hwl.commonutils.utils.JwtUtils;
import com.hwl.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-10-08
 */
@Api(tags = "网站订单")
@RestController
@RequestMapping("/api/order/auth")
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation("新增订单")
    @PostMapping("save/{courseId}")
    public RespBean save(@PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        //返回订单号
        return RespBean.ok().data(orderService.saveOrder(courseId, jwtInfo.getId()));
    }

    @ApiOperation("根据订单id获取订单")
    @GetMapping("get/{orderId}")
    public RespBean get(@PathVariable String orderId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        //返回订单信息
        return RespBean.ok().data(orderService.getOrderById(orderId, jwtInfo.getId()));
    }

    @ApiOperation("判断课程是否购买")
    @GetMapping("isBuy/{courseId}")
    public RespBean isBuyByCourseId(@PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        //返回布尔值
        return RespBean.ok().data(orderService.isBuyByCourseId(courseId, jwtInfo.getId()));
    }

    @ApiOperation("查询当前用户的订单列表")
    @GetMapping("list")
    public RespBean list(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        return RespBean.ok().data(orderService.selectByMemberId(jwtInfo.getId()));
    }

    @ApiOperation("用户删除订单")
    @DeleteMapping("remove/{orderId}")
    public RespBean remove(@PathVariable String orderId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (orderService.removeById(orderId, jwtInfo.getId())) {
            return RespBean.ok().message("订单删除成功！");
        }
        return RespBean.error().message("订单删除失败！");
    }

    //前端的定时任务改成了另一个接口
    @ApiOperation("查询支付状态")
    @GetMapping("queryOrderStatus/{orderNo}")
    public RespBean queryOrderStatus(@PathVariable String orderNo) {
        if (orderService.queryOrderStatus(orderNo)) {
            return RespBean.ok().message("支付成功！");
        }
        return RespBean.setResult(ResultCodeEnum.PAY_RUN); //支付中
    }
}

